<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class Template extends AppModel {

/**
 * Defining the name of model
 *
 */
    var $name = 'Template';
/**
 * Defining the name of the table
 *
 */
    var $useTable = 'templates';


    function saveCommittee($committee_id) {
        
        $templates=$this->find('all',array('conditions'=>array('model'=>'System')));
        foreach($templates as $template){
            $ctemplate['type']=$template['Template']['type'];
            $ctemplate['title']=$template['Template']['title'];
            $ctemplate['description']=$template['Template']['description'];
            $ctemplate['template']=$template['Template']['template'];
            $ctemplate['foreign_key']=$committee_id;
            $ctemplate['model']='Committee';
            $this->create();
            $this->save($ctemplate);
        } 
    }
    
    /**
     * Describe findTemplate
     * find template 
     * @param $type
     * @param $data
     * @return $template
     */
    function findTemplate($type,$data){
        $template=$this->find('first',array('conditions'=>array('model'=>'Meeting','foreign_key'=>$data['id'],'type'=>$type)));
        if(!$template){
            $template=$this->find('first',array('conditions'=>array('model'=>'Committee','foreign_key'=>$data['committee_id'],'type'=>$type)));
        }
        return $template;
    }
    /**
     * Describe processTemplate
     * find template and replace it with data
     * @param $type
     * @param $data
     * @return $template
     */
    function processTemplate($type,$data){
        $template=$this->findTemplate($type,$data['Meeting']);
        $data['slogan'] = '"'.strtoupper(Configure::read('agency_slogan')).'"';
        $template['Template']['template']=$this->replacetemplate($template['Template']['template'],$data);
        return $template;
    }
}
?>
